package com.young.android.entity;

import android.database.Cursor;

/**
 * 基础父类
 * @author Administrator
 *
 */
public class BaseClass {

    /**
     * 导入数据集
     * 
     * @param cur
     * @return
     */
    public boolean LoadFormCursor(Cursor cur)
    {
      boolean breturn = false;
      try
      {
        java.lang.reflect.Field[] fields = this.getClass().getFields();
        int ilen = fields.length;
        for (int i = 0; i < ilen; i++)
        {
          Class<?> atype = fields[i].getType();
          String aname = fields[i].getName();
          int columnindex = cur.getColumnIndex(aname);
          if (columnindex >= 0)
          {
            String avalue = null;
            try
            {
              avalue = cur.getString(columnindex);
            } catch (Exception e)
            {
              // TODO: handle exception
              e.printStackTrace();
            }
            if ((avalue != null) && (!avalue.equals("")) && (!avalue.equals("null")))
            {
              if (atype.equals(double.class))
              {
                fields[i].setDouble(this, cur.getDouble(columnindex));
              } else if (atype.equals(int.class))
              {
                fields[i].setInt(this, cur.getInt(columnindex));
              } else if (atype.equals(long.class))
              {
                fields[i].setLong(this, cur.getLong(columnindex));
              } else if (atype.equals(boolean.class))
              {
                if ((avalue.equals("1")) || (avalue.equals("0")))
                  // 数据库保存的是 0/1
                  fields[i].setBoolean(this, avalue.equals("1"));
                else
                  // 数据库保存的是 true/false
                  fields[i].setBoolean(this, Boolean.parseBoolean(avalue));
              } else
              {
                fields[i].set(this, avalue);
              }
            }
          }
        }
        breturn = true;
      } catch (IllegalArgumentException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IllegalAccessException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      return breturn;
    }
    
}
